package com.zzs.client.convert;

import com.zzs.client.pojo.dto.submit.CodeSubmitDTO;
import com.zzs.client.pojo.vo.submit.CodeSubmitDetailVO;
import com.zzs.client.pojo.vo.submit.CodeSubmitVO;
import com.zzs.common.core.entity.msg.CodeSubmitMessage;
import com.zzs.common.core.entity.mysql.CodeSubmitDO;
import com.zzs.common.core.entity.redis.CodeSubmitCache;
import com.zzs.common.core.util.SnowflakeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zzs
 * @date 2025/04/13
 */
@Component
public class CodeSubmitConvert {
    public CodeSubmitMessage dtoToMessage(CodeSubmitDTO codeSubmitDTO, long userId) {
        CodeSubmitMessage message = new CodeSubmitMessage();
        BeanUtils.copyProperties(codeSubmitDTO, message);
        message.setUserId(userId);
        message.setId(SnowflakeUtil.nextId());
        return message;
    }

    public CodeSubmitCache doToCache(CodeSubmitDO codeSubmitDO) {
        CodeSubmitCache codeSubmitCache = new CodeSubmitCache();
        BeanUtils.copyProperties(codeSubmitDO, codeSubmitCache);
        return codeSubmitCache;
    }

    public CodeSubmitDetailVO doToVo(CodeSubmitDO codeSubmitDO) {
        CodeSubmitDetailVO codeSubmitDetailVO = new CodeSubmitDetailVO();
        BeanUtils.copyProperties(codeSubmitDO, codeSubmitDetailVO);
        long useMemory = codeSubmitDetailVO.getUseMemory();
//        把B转换成MB, 对用户来说更加直观
        codeSubmitDetailVO.setUseMemory(useMemory / 1024 / 1024);
        return codeSubmitDetailVO;
    }

    public List<CodeSubmitVO> doListToVoList(List<CodeSubmitDO> codeSubmitDOList) {
        return codeSubmitDOList.stream()
                .map(codeSubmitDO -> {
                    CodeSubmitVO codeSubmitVO = new CodeSubmitVO();
                    BeanUtils.copyProperties(codeSubmitDO, codeSubmitVO);
                    long useMemory = codeSubmitVO.getUseMemory();
//                  把B转换成MB, 对用户来说更加直观
                    codeSubmitVO.setUseMemory(useMemory / 1024 / 1024);
                    return codeSubmitVO;
                }).toList();
    }
}
